
import TSMap from "../struct/TSMap";
import XlsxData from "../xlsx/XlsxData";
import DataItemModel from "./DataItemModel";
//model 容器 处理配置数据
export default class ModelManager<T extends DataItemModel> {

    protected itemMap: TSMap<string | number, T> = new TSMap()
    protected itemList: T[]

    protected data: XlsxData;

    getData(): XlsxData {
        return this.data;
    }

    initWithData(data: XlsxData, type: { new(): T; }) {
        this.data = data;
        // cc.log(' data size ',data.size())
        data.forEach((key, value: any[]) => {
            let ins = new type();
            ins.init(key, value)
            this.itemMap.set(key, ins)
        })
        this.itemList = this.itemMap.getArray()
        // cc.log(' this.itemList ',this.itemList.length)
    }

    getByIndex(index: number) {
        if (index >= 0 && index <= this.itemList.length - 1) {
            return this.itemList[index]
        }
    }

    getByID(id: string | number) {
        return this.itemMap.get(id)
    }

    size() {
        return this.itemMap.size()
    }


    getList() {
        return this.itemList;
    }

    getMap() {
        return this.itemMap;
    }


    clear() {
        this.data = null;
        this.itemMap.clear()
        this.itemList.length = 0;
    }
}
